package com.common;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.util.StringUtil;

public class StringUtils {

	public static Map<String, String> getStringToMap(String str) {
		// 感谢bojueyou指出的问题
		// 判断str是否有值
		if (null == str || "".equals(str)) {
			return null;
		}
		// 根据&截取
		String[] strings = str.split(",");
		// 设置HashMap长度
		int mapLength = strings.length;
		// 判断hashMap的长度是否是2的幂。
		if ((strings.length % 2) != 0) {
			mapLength = mapLength + 1;
		}

		Map<String, String> map = new HashMap();
		// 循环加入map集合
		for (int i = 0; i < strings.length; i++) {
			// 截取一组字符串
			String[] strArray = strings[i].split("=");
			// strArray[0]为KEY strArray[1]为值
			if (strArray.length == 2) {
				map.put(strArray[0].trim(), strArray[1]);
			} else {
				map.put(strArray[0].trim(), "");
			}
		}
		return map;
	}

	public static String sideTrim(String stream, String trimstr) {
		//
		if (stream == null || stream.length() == 0 || trimstr == null || trimstr.length() == 0) {
			return stream;
		}

		//
		int epos = 0;

		//
		String regpattern = "[" + trimstr + "]*+";
		Pattern pattern = Pattern.compile(regpattern, Pattern.CASE_INSENSITIVE);

		//
		StringBuffer buffer = new StringBuffer(stream).reverse();
		Matcher matcher = pattern.matcher(buffer);
		if (matcher.lookingAt()) {
			epos = matcher.end();
			stream = new StringBuffer(buffer.substring(epos)).reverse().toString();
		}

		//
		matcher = pattern.matcher(stream);
		if (matcher.lookingAt()) {
			epos = matcher.end();
			stream = stream.substring(epos);
		}

		//
		return stream;
	}

	public static boolean IsRepeat(String[] yourValue) {
		boolean value = false;
		for (int i = 0; i < yourValue.length - 1; i++) {
			for (int j = i + 1; j < yourValue.length - 1; j++) {
				if (yourValue[i].equals(yourValue[j])) {
					value = true;
				}
			}
		}
		return value;
	}

	//
	public static int parseInt(String s) {
		if (s == null || s.equals("")) {
			return 0;
		} else {
			return Integer.parseInt(s);
		}
	}

	//
	public static BigDecimal parseBD(String s) {
		if (s == null || s.equals("")) {
			BigDecimal bDecimal = new BigDecimal(0);
			return bDecimal;
		} else {
			BigDecimal bDecimal = new BigDecimal(s);
			return bDecimal;
		}
	}
}
